package com.cq.hd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cq.hd.admin.dto.AdminUserDto;
import com.cq.hd.admin.mapper.TbAdminUserMapper;
import com.cq.hd.admin.po.TbAdminUserPo;
import com.cq.hd.admin.service.TbAdminUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cq.hd.admin.vo.AdminUserDetailVo;
import com.cq.hd.common.constant.RedisKeyConstant;
import com.cq.hd.common.enums.biz.DeleteStatusEnum;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.utils.BeanUtils;
import com.cq.hd.common.utils.LocalDateUtil;
import com.cq.hd.common.utils.RedisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mYunYu
 * @since 2021-10-28
 */
@Service
public class TbAdminUserServiceImpl extends ServiceImpl<TbAdminUserMapper, TbAdminUserPo> implements TbAdminUserService {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public TbAdminUserPo getUserByUserName(String userName) {
        return this.baseMapper.selectOne(new QueryWrapper<TbAdminUserPo>()
                .eq("user_name", userName)
                .eq("delete_status", DeleteStatusEnum.NORMAL.getValue())
                .last("limit 1"));
    }

    @Override
    public Page<AdminUserDetailVo> getAdminUserList(String userName, String userRole, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AdminUserDetailVo> adminUserDetailVoList = this.baseMapper.listAdminUserByDto(userName, userRole);

        PageInfo<AdminUserDetailVo> adminUserDetailVoPageInfo = new PageInfo<>(adminUserDetailVoList);
        Page<AdminUserDetailVo> adminUserDetailVoPage = new Page<>();
        BeanUtils.copyProperties(adminUserDetailVoPageInfo, adminUserDetailVoPage);

        return adminUserDetailVoPage;
    }

    @Override
    public void addAdminUser(AdminUserDto adminUserDto) {
        if (StringUtils.isEmpty(adminUserDto.getAvatar())) {
            // 默认头像
            adminUserDto.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        }
        adminUserDto.setCreateTime(LocalDateUtil.getNowLocalDateTime());
        adminUserDto.setUpdateTime(LocalDateUtil.getNowLocalDateTime());
        TbAdminUserPo userEntity = new TbAdminUserPo();
        BeanUtils.copyProperties(adminUserDto, userEntity);
        baseMapper.insert(userEntity);
    }

    @Override
    public void deleteAdminUser(Integer id) {
        TbAdminUserPo adminUserPo = new TbAdminUserPo();
        adminUserPo.setId(id);
        adminUserPo.setDeleteStatus(DeleteStatusEnum.DELETED.getValue());
        int update = baseMapper.updateById(adminUserPo);
        if (update > 0) {
            // 清空redis缓存
            redisUtil.del(String.format(RedisKeyConstant.ADMIN_USER_LOGIN_KEY, String.valueOf(adminUserPo.getId())));
        }
    }

    @Override
    public int updateAdminUserById(AdminUserDto adminUserDto) {
        adminUserDto.setUpdateTime(LocalDateUtil.getNowLocalDateTime());
        TbAdminUserPo adminUserPo = new TbAdminUserPo();
        org.springframework.beans.BeanUtils.copyProperties(adminUserDto, adminUserPo);

        int update = baseMapper.updateById(adminUserPo);
        if (update > 0) {
            // 用户状态：0：正常；1：封禁
            if (adminUserDto.getStatus() == 1) {
                // 清空redis缓存
                redisUtil.del(String.format(RedisKeyConstant.ADMIN_USER_LOGIN_KEY, String.valueOf(adminUserDto.getId())));
            }
        }

        return update;
    }

    @Override
    public int updateAvatarById(String avatar, Integer id) {
        TbAdminUserPo adminUserPo = new TbAdminUserPo();
        adminUserPo.setId(id);
        adminUserPo.setAvatar(avatar);
        adminUserPo.setUpdateTime(LocalDateTime.now());
        return this.baseMapper.updateById(adminUserPo);
    }

    @Override
    public TbAdminUserPo getUserByMobile(String mobile) {
        return this.baseMapper.selectOne(new QueryWrapper<TbAdminUserPo>()
                .eq("mobile", mobile)
                .eq("delete_status", DeleteStatusEnum.NORMAL.getValue())
                .last("limit 1"));
    }
}
